package logic

import (
	"context"
	"renting-demo/model/mysql"
	"strconv"

	"renting-demo/cart_srv/cart"
	"renting-demo/cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RemoveLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRemoveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveLogic {
	return &RemoveLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// todo: 删除、移除购物车
func (l *RemoveLogic) Remove(in *cart.PruductRemoveCartReq) (*cart.Response, error) {
	if in.GoodsId == 0 {
		num := mysql.DelAll(strconv.Itoa(int(in.UserId)))
		for i, s := range num {
			mysql.BackStock(strconv.Itoa(int(in.UserId)), i, s)
		}
		return &cart.Response{
			Status: "1",
		}, nil
	}
	mysql.DelPart(strconv.Itoa(int(in.UserId)), strconv.Itoa(int(in.GoodsId)), int(in.GoodsNum))
	mysql.BackStock(strconv.Itoa(int(in.UserId)), strconv.Itoa(int(in.GoodsId)), int(in.GoodsNum))
	return &cart.Response{
		Status: "2",
	}, nil
}
